<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./css/index.css" />
  </head>
  <body>
    <div class="container">
      <ul class="top"></ul>
    </div>
    <script src="../axios.js"></script>
    <script>
      axios.defaults.baseURL = 'http://ajax-api.itheima.net'
      const listTop = document.querySelector('.top')
      axios({
        url: '/api/category/top',
        method: 'GET',
      })
        .then(({ data: res }) => {
          const axiosArr = res.data.map((item) => {
            return axios({
              url: '/api/category/sub?id=' + item.id,
              method: 'GET',
            })
          })
          return Promise.all(axiosArr)
        })
        .then((res) => {
          const html = res
            .map(({ data: item }) => {
              return `<li>
          <a href="javascript:;">${item.data.name}</a>
          <img
            src="${item.data.picture}"
            alt=""
          />
          <ul class="sub">
           ${item.data.children
             .map((item) => {
               return `<li>
              <a href="javascript:;">
                <span>${item.name}</span>
                <img
                  src="${item.picture}"
                  alt=""
                />
              </a>
            </li>`
             })
             .join('')}
          </ul>
        </li>`
            })
            .join('')
          listTop.innerHTML = html
        })
    </script>
  </body>
</html>
